package com.ilancuo.string;

/**
 * 测试一个字节数组在另外一个字节数组中的位置
 * <p>
 * Created by ilancuo on 12/03/2017.
 */
public class ByteArray {

    public static int getIndex(byte[] b, byte[] bb) {
        if (b == null || bb == null || b.length == 0 || bb.length == 0 || b.length < bb.length)
            return -1;
        int i, j;
        for (i = 0; i < b.length - bb.length; i++) {
            if (b[i] == bb[0]) {
                for (j = 1; j < bb.length; j++) {
                    if (b[i + j] != bb[j])
                        break;
                }
                if (j == bb.length)
                    return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        byte[] a = {'A', 'B', 'C', 'D', 'E'};
        byte[] b = {'B', 'C', 'E'};
        System.out.println(getIndex(a, b));
    }
}
